// 上传作业
function uploadFile() {
	var formData = new FormData($("#ff")[0]);
	$.ajax({
		type : "post",
		url : "student_upload.do",
		data : formData,
		processData : false,
		// 必须false才会自动加上正确的Content-Type
		contentType : false,
		cache : false,
		dataType : "json",
		success : function(json) {
			getStudent();
			getMaterial();
			getHomeWork();
		}
	});
}
function getStudent() {
	$.ajax({
		type : "get",
		datatype : "text",
		url : "getStudent.do",
		async : true,
		success : function(json) {
			var stu = $.parseJSON(json);
			if(stu.good==null){
				stu.good=0;
			}
			var stat ='';
			if( stu.stats === 'C' ){
				stat ='<img src="img/late2.png"/>';
			}
			else {
				stat = stu.stats;
			}
			
			var stuxx = "<tr>" + "<td>" + stu.username + "</td>" + "<td>" + stu.uid + "</td>" + "<td>" + stu.good + "</td>" + "<td>" + stat + "</td>" + "</tr>";
			toBody =stuxx;
			$("#myTb-1-1").html("");
			$("#myTb-1-1").append(toBody);
		}
	});
}

function getMaterial() {
	// 教师发布上课资料
	$.ajax({
		type : "get",
		datatype : "text",
		url : "list_Material.do",
		async : true,
		success : function(result) {
			var data = $.parseJSON(result);
			var temp = "";
			for (var i = 0; i < data.length; i++) {
				var obj = data[i];
				var idStr = obj.id;
				var urlStr = obj.material;
				temp += '<tr><td hidden ="true">' + idStr + '</td><td>'	+ (i + 1) + '</td><td><a href="javascript:downloadMaterial(\''+idStr+'\')">' + urlStr + '</a></td></tr>';
			}
			$("#myTb-2").html(temp);
		}
	});
}
function downloadMaterial(value){
	$.get("download.do",{id:value}, function(result) {
		var data =  $.parseJSON(result);
		downloadByIo(data.msg);
//		window.location.href = data.msg;
	});
}

function downloadByIo(str){
	window.location.href="downloadByIo?url="+str;
}

function getHomeWork(){
	$.ajax({
		type: "get",
		datatype: "json",
		url:"list_homework.do",
		async: true,
		success: function(result) {
			var data =  $.parseJSON(result);
			var temp = "";
			for(var item in data) {
				var no = data[item].id;
				var fileName = data[item].fileName;
				var fileSize = data[item].fileSize;
				var uploadTime = data[item].uploadTime;
				temp += '<tr><td hidden="true">'+no+'</td><td>'+ fileName +'</td><td>'+ fileSize + 'K' +'</td><td>'+ uploadTime +'</td></tr>';
			}
			tbBody =temp;
			$("#myTb-3-3").html("");
			$("#myTb-3-3").append(tbBody);

			var obj = document.getElementById('fileupload') ;
			obj.outerHTML=obj.outerHTML;       
		}
	});
}
